package com.huoxingy.statics.services;

import com.huoxingy.statics.repo.DailyMemberLostRepo;
import com.huoxingy.statics.repo.TotalMemberConsumptionRepo;
import com.huoxingy.statics.vo.TotalMemberConsumptionsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * Created by lihu on 2018/1/8.
 */
@Service
public class TotalMemberConsumptionServiceImpl implements TotalMemberConsumptionService {

    @Autowired
    TotalMemberConsumptionRepo totalMemberConsumptionRepo;

    @Autowired
    DailyMemberLostRepo dailyMemberLostRepo;


    @Override
    public TotalMemberConsumptionsVo getTotalMemberConsumption(String storeId, Pageable pageable) {
        String bizDate = getBizDate(-1);
        Long totalElements = totalMemberConsumptionRepo.queryAllByStoreIdAndBizDate(storeId, bizDate);
        if (totalElements == 0) {
            bizDate = getBizDate(-2);
            totalElements = totalMemberConsumptionRepo.queryAllByStoreIdAndBizDate(storeId, bizDate);
        }
        int totalPages = (int) Math.ceil(totalElements * 1d / pageable.getPageSize());
        List<Map<String, Object>> consumptions = totalMemberConsumptionRepo.findByStoreIdAndBizDate(storeId, bizDate, pageable);
        TotalMemberConsumptionsVo consumptionsVo = new TotalMemberConsumptionsVo(totalPages, totalElements);
        consumptions.forEach(e -> consumptionsVo.getConsumptions().add(new TotalMemberConsumptionsVo.
                TotalMemberConsumption(e.get("memberId").toString(), Long.parseLong(e.get("orderCount").toString()),
                Long.parseLong(e.get("amount").toString()))));
        return consumptionsVo;
    }

    @Override
    public TotalMemberConsumptionsVo getLostMemberConsumption(String storeId, Pageable pageable) {
        String bizDate = getBizDate(-1);
        Long totalElements = dailyMemberLostRepo.getLossMember(storeId, bizDate);
        if (totalElements == 0) {
            bizDate = getBizDate(-2);
            totalElements = dailyMemberLostRepo.getLossMember(storeId, bizDate);
        }
        List<Map<String, Object>> consumptions = totalMemberConsumptionRepo.queryLostInfo(storeId, bizDate, pageable);
        int totalPages = (int) Math.ceil(totalElements * 1d / pageable.getPageSize());
        TotalMemberConsumptionsVo consumptionsVo = new TotalMemberConsumptionsVo(totalPages, totalElements);
        consumptions.forEach(e -> consumptionsVo.getConsumptions().add(new TotalMemberConsumptionsVo.
                TotalMemberConsumption(e.get("memberId").toString(), Long.parseLong(e.get("orderCount").toString()),
                Long.parseLong(e.get("amount").toString()))));
        return consumptionsVo;
    }

    @Override
    public List<Map<String, Object>> getLostMemberIds(String storeId) {
        String bizDate = getBizDate(-1);

        List<Map<String, Object>> list = dailyMemberLostRepo.getMemberIds(storeId, bizDate);
        if (list.size() == 0) {
            bizDate = getBizDate(-2);
            list = dailyMemberLostRepo.getMemberIds(storeId, bizDate);
        }
        return list;
    }


    private String getBizDate(int i) {
        OffsetDateTime d = OffsetDateTime.now(ZoneId.of("Asia/Shanghai"));
        d = d.plusDays(i);
        return d.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

    }


}
